package 双指针;

public class 反转字符串中的单词3 {
    public String reverseWords(String s) {
        String[] s1 = s.split(" ");
        StringBuilder builder = new StringBuilder();
        for (String s2 : s1) {
            builder.append(reverse(s2)).append(" ");
        }
        return builder.toString().trim();
    }

    private String reverse(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        for (int i = 0,j = n-1;i<j;i++,j--){
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
        return new String(chars);
    }
}
